package org.summer.lawyer.controller.wx;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.exception.ServiceException;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.constant.SystemConstants;
import org.summer.lawyer.domain.bo.lawfirmtag.LawFirmTagBo;
import org.summer.lawyer.domain.bo.spucomment.SpuCommentBo;
import org.summer.lawyer.domain.vo.lawfirmtag.LawFirmTagVo;
import org.summer.lawyer.domain.vo.spucomment.SpuCommentVo;
import org.summer.lawyer.service.ILawFirmTagService;
import org.summer.lawyer.service.ISpuCommentService;

/**
 *
 */
@Tag(name = "微信小程序服务评价相关接口", description = "商品评价信息")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wx/spuComment")
public class WxSpuCommentController extends BaseController {

    private final ISpuCommentService spuCommentService;

    private final ILawFirmTagService lawFirmTagService;

    /**
     * 查询商品评价信息列表
     */
    @Parameters({
            @Parameter(name = "spuId", description = "商品ID", in = ParameterIn.QUERY, required = true)
    })
    @Operation(summary = "查询商品评价信息列表", description = "查询商品评价信息列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaIgnore
    @GetMapping("/list")
    public TableDataInfo<SpuCommentVo> list(SpuCommentBo bo, PageQuery pageQuery){
        bo.setStatus("1");
        return spuCommentService.selectPageList(bo,pageQuery);
    }

    /**
     * 新增商品评价信息
     */
    @Parameters({
            @Parameter(name = "wxUserId", description = "用户ID", required = true),
            @Parameter(name = "orderId", description = "订单ID", required = true),
            @Parameter(name = "overallRating", description = "综合评价", required = true),
            @Parameter(name = "competenceRating", description = "专业水平", required = true),
            @Parameter(name = "attitudeRating", description = "工作态度", required = true),
            @Parameter(name = "efficiencyRating", description = "工作效率", required = true),
            @Parameter(name = "reviewContent", description = "评价内容", required = true),
            @Parameter(name = "reviewTag", description = "印象标签,多个用“,”分隔", required = true),
            @Parameter(name = "lawFirmId", description = "律所ID，评价律师的时候要传", required = false)
    })
    @Operation(summary = "新增商品评价信息", description = "新增商品评价信息", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckLogin
    @Log(title = "商品评价信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveSpuComment")
    public R<Void> saveSpuComment(@Validated(AddGroup.class) @RequestBody SpuCommentBo bo) {
        if(bo.getLawFirmId() != null){
            //查询评价今日评价次数
            Integer commentCount = spuCommentService.countCommentByDay(bo);
            if(commentCount >= SystemConstants.COMMENT_LAWYER_MAX_COUNT){
                throw new ServiceException("今天的评价次数已经用完了，明天再来吧");
            }
        }
        return toAjax(spuCommentService.insertByBo(bo));
    }

    @Operation(summary = "获取评价标签列表", description = "律所律所评价标价， 直接使用标签名称", responses = { @ApiResponse( responseCode = "200", description = "成功") })
    @SaIgnore
    @GetMapping("/listTag")
    public TableDataInfo<LawFirmTagVo> listTag(){
        return TableDataInfo.build(lawFirmTagService.selectLawFirmTagVoList(new LawFirmTagBo()));
    }
}
